#[macro_use] extern crate glium;

mod pulse;

use glium::Surface;

const SAMPLE_RATE: u32 = 48000;
const AUDIO_BUFFER_SIZE: usize = 512;

const VERTEX_SHADER: &'static str = include_str!("shaders/vertex.glsl");
const FRAGMENT_SHADER: &'static str = include_str!("shaders/fragment.glsl");


#[derive(Copy, Clone, Debug)]
struct Vertex {
    position: [f32; 2]
}

implement_vertex!(Vertex, position);

fn main() {
    use glium::DisplayBuild;
    let display = glium::glutin::WindowBuilder::new()
        .with_dimensions(1024, 768)
        .with_title(format!("Viz Thing"))
        .build_glium()
        .unwrap();
    let handle = pulse::start_thread(SAMPLE_RATE, AUDIO_BUFFER_SIZE);
    let program = glium::Program::from_source(&display, VERTEX_SHADER, FRAGMENT_SHADER, None).unwrap();
    loop {
        let mut target = display.draw();
        target.clear_color(0., 0., 0., 1.);
        let data = handle.get_data();
        let shape: Vec<Vertex> = data.iter().enumerate().map(|(i, val)| Vertex {position: [(i as f32 / AUDIO_BUFFER_SIZE as f32) * 2. - 1., val[0]]}).collect();
        let vb = glium::VertexBuffer::new(&display, &shape).unwrap();
        let ib = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
        target.draw(&vb, &ib, &program, &glium::uniforms::EmptyUniforms, &Default::default()).unwrap();
        target.finish().unwrap();
        for ev in display.poll_events() {
        }
    }
}
